package com.iris.netty.netty5;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

public class ServerHandler extends SimpleChannelInboundHandler {

    @Override
    protected void messageReceived(ChannelHandlerContext channelHandlerContext, Object msg) throws Exception {
//        ByteBuf buf = (ByteBuf)msg;
//        byte[] data = new byte[buf.readableBytes()];
//        buf.readBytes(data);
//        String request = new String(data, "utf-8");
//        System.out.println("Server: " + request);
//        //写给客户端
//        String response = "我是反馈的信息";
//        channelHandlerContext.writeAndFlush(Unpooled.copiedBuffer("888".getBytes()));
        if(msg instanceof ByteBuf){
            ByteBuf buf = (ByteBuf)msg;
            byte[] data = new byte[buf.readableBytes()];
            buf.readBytes(data);
            String request = new String(data, "utf-8");
            System.out.println("Server: ByteBuf===:" + request);
        }

        System.out.println("Server: " + msg);
        channelHandlerContext.writeAndFlush("888");
    }


    @Override
    public void userEventTriggered(final ChannelHandlerContext ctx, Object evt) throws Exception {
        System.out.println("=====userEventTriggered==="+evt.toString());
        if(evt instanceof IdleStateEvent){
            IdleStateEvent event = (IdleStateEvent)evt;
            System.out.println("=====userEventTriggered==="+event.state());
            if(event.state() == IdleState.ALL_IDLE){

                //清除超时会话
                ChannelFuture writeAndFlush = ctx.writeAndFlush("you will close");
                writeAndFlush.addListener((ChannelFutureListener) future -> {
                    ctx.channel().close();
                    System.out.println("close ===="+ctx.channel());
                });

//                writeAndFlush.addListener(new ChannelFutureListener() {
//
//                    @Override
//                    public void operationComplete(ChannelFuture future) throws Exception {
//                        ctx.channel().close();
//                    }
//                });
            }
        }else{
            System.out.println("else===");
            super.userEventTriggered(ctx, evt);
        }
    }

    /**
     * 新客户端接入
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channelActive");
    }

    /**
     * 客户端断开
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channelInactive");
        ctx.channel().close();
    }

    /**
     * 异常
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
    }

}
